/*  Novelties: A visual novel programming language based on JavaFX
    Copyright (C) 2008  Hao Xu

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * /
/*
 * Animation.fx
 *
 * Created on Apr 6, 2008, 9:30:24 PM
 */

package avgengine;
import javafx.ui.canvas.*;
import javafx.ui.*;
import java.lang.Thread;

/**
 * @author ertri
 */

class Animation {
    operation translate(target:Translatable, newcoord:int*, duration:int,cont:operation());
    operation resize(target:Resizable, newsize:int*, duration:int, cont:operation());
    operation fadeIn(target:Node, step:Number, duration:int, cont:operation());
    operation fadeOut(target:Node, step:Number, duration:int, cont:operation());
    operation justFadeIn(target:Node, step:Number, duration:int);
    operation justFadeOut(target:Node, step:Number, duration:int);
    operation justChangeOpacity(target:Node, op:Number, step:Number, duration:int);
    operation justResize(target:Resizable, newsize:int*, duration:int);
    operation justTranslate(target:Translatable, newcoord:int*, duration:int);
}


operation Animation.fadeIn(target, step, duration, cont) {
    var opacity = target.opacity;
    var a = 0.00;
    var b = 1.00/step;
    var c = 1.00;
    target.opacity = [a,b..c] dur duration;
    do later {
        Thread.sleep(duration);
        cont();
    }
}

operation Animation.fadeOut(target, step, duration, cont) {
    var a = target.opacity;
    var b = a - a/step;
    var c = 0.00;
    target.opacity = [a,b..c] dur duration;
    do later {
        Thread.sleep(duration);
        cont();
    }
}

operation Animation.justFadeIn(target, step, duration) {
    var opacity = target.opacity;
    var a = 0.00;
    var b = 1.00/step;
    var c = 1.00;
    target.opacity = [a,b..c] dur duration;
}

operation Animation.justChangeOpacity(target, op, step, duration) {
    var opacity = target.opacity;
    var a = target.opacity;
    var b = (op-target.opacity)/step+target.opacity;
    var c = op;
    target.opacity = [a,b..c] dur duration;
}

operation Animation.justFadeOut(target, step, duration) {
    var a = target.opacity;
    var b = a - a/step;
    var c = 0.00;
    target.opacity = [a,b..c] dur duration;
}
operation Animation.resize(target, newsize, duration, cont) {
    
    var a = target.dwidth;
    if(a<>newsize[0]) {
        var b = if a>newsize[0] then a-1 else a+1;
        var c = newsize[0];
        target.dwidth = [a,b..c] dur duration;
    }
    a = target.dheight;
    if(a<>newsize[1]) {
        var b = if a>newsize[1] then a-1 else a+1;
        var c = newsize[0];
        target.dheight=[a,b..c] dur duration;
    }
    do later {
        Thread.sleep(duration);
        cont();
    }
}

operation Animation.justResize(target, newsize, duration) {
    var a = target.dwidth;
    if(a<>newsize[0]) {
        var b = if a>newsize[0] then a-1 else a+1;
        var c = newsize[0];
        target.dwidth = [a,b..c] dur duration;
    }
    a = target.dheight;
    if(a<>newsize[1]) {
        var b = if a>newsize[1] then a-1 else a+1;
        var c = newsize[1];
        target.dheight=[a,b..c] dur duration;
    }
}
operation Animation.translate(target, newsize, duration, cont) {
    
    var a = target.x;
    if(a<>newsize[0]) {
        var b = if a>newsize[0] then a-1 else a+1;
        var c = newsize[0];
        target.x = [a,b..c] dur duration;
    }
    a = target.y;
    if(a<>newsize[1]) {
        var b = if a>newsize[1] then a-1 else a+1;
        var c = newsize[0];
        target.y=[a,b..c] dur duration;
    }
    do later {
        Thread.sleep(duration);
        
        cont();
    }
}

operation Animation.justTranslate(target, newsize, duration) {
    var a = target.x;
    if(a<>newsize[0]) {
        var b = if a>newsize[0] then a-1 else a+1;
        var c = newsize[0];
        target.x = [a,b..c] dur duration;
    }
    a = target.y;
    if(a<>newsize[1]) {
        var b = if a>newsize[1] then a-1 else a+1;
        var c = newsize[1];
        target.y=[a,b..c] dur duration;
    }
}
